/*
 * @Author: zhiming
 * @Date: 2020-07-20 14:56:16
 * @LastEditTime: 2020-08-04 17:32:54
 * @LastEditors: Please set LastEditors
 * @Description: 工具函数类
 * @FilePath: \shopping_mall_app\src\assets\js\utils\index.js
 */
import Echo from '../libs/echo.js';
import moment from 'moment';

//获取路由参数
export function localParam (search, hash) {
    search = search || window.location.search;
    hash = hash || window.location.hash;
    var fn = function(str, reg) {
        if (str) {
            var data = {};
            str.replace(reg, function($0, $1, $2, $3) {
                data[$1] = $3;
            });
            return data;
        }
    };
    return {
        search : fn(search, new RegExp("([^?=&]+)(=([^&]*))?", "g")) || {},
        hash : fn(hash, new RegExp("([^#=&]+)(=([^&]*))?", "g")) || {}
    };
}

//页面置顶
export function setScrollTop(val=0){
    setTimeout(()=>{
        document.body.scrollTop=val;
        document.documentElement.scrollTop=val;
    },300);
}

//图片懒加载
export function lazyImg(){
    Echo.init({
        offset: 50, //离可视区域多少像素的图片可以被加载
        throttle: 0 //图片延迟多少毫秒加载
    });
}

//页面跳转
export function goPage(url){
    this.props.history.push(window.base.config.path+url);
}

//数据深拷贝
export function deepClone(obj, hash = new WeakMap()){
    if (obj === null) return obj; // 如果是null或者undefined我就不进行拷贝操作
    if (obj instanceof Date) return new Date(obj);
    if (obj instanceof RegExp) return new RegExp(obj);
    // 可能是对象或者普通的值  如果是函数的话是不需要深拷贝
    if (typeof obj !== "object") return obj;
    // 是对象的话就要进行深拷贝
    if (hash.get(obj)) return hash.get(obj);
    let cloneObj = new obj.constructor();
    // 找到的是所属类原型上的constructor,而原型上的 constructor指向的是当前类本身
    hash.set(obj, cloneObj);
    // eslint-disable-next-line
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
        // 实现一个递归拷贝
        cloneObj[key] = deepClone(obj[key], hash);
        }
    }
    return cloneObj;
}

//组织冒泡事件
export function preventBubble(e){
    e.stopPropagation();
}

//封装的日期排序方法
export function ForwardRankingDate(data, p) {
    for (let i = 0; i < data.length - 1; i++) {
        for (let j = 0; j < data.length - 1 - i; j++) {
            if (Date.parse(data[j][p]) > Date.parse(data[j+1][p])) {
                let temp = data[j];
                data[j] = data[j + 1];
                data[j + 1] = temp;
            }
        }
    }
    return data;
}

//获取最近的8个时间段(半个小时为间断)
export function getLatestHours(){
    const hour = moment().format('HH');
    const time1 = moment(hour, 'HH').subtract(1.5, 'hour').format('HH:mm');
    const time2 = moment(hour, 'HH').subtract(1, 'hour').format('HH:mm');
    const time3 = moment(hour, 'HH').subtract(0.5, 'hour').format('HH:mm');
    const time4 = moment(hour, 'HH').add(0.5, 'hour').format('HH:mm');
    const time5 = moment(hour, 'HH').add(1, 'hour').format('HH:mm');
    const time6 = moment(hour, 'HH').add(1.5, 'hour').format('HH:mm');
    const time7 = moment(hour, 'HH').add(2, 'hour').format('HH:mm');
    const hours = [
        { time: time1, hide: false }, 
        { time: time2, hide: false }, 
        { time: time3, hide: false }, 
        { time: `${hour}:00`, hide: false },
        { time: time4, hide: false }, 
        { time: time5, hide: false }, 
        { time: time6, hide: false }, 
        { time: time7, hide: false }
    ];
    return hours;
}

//以半小时为间隔添加时间段
export function addTime(time, count){
    let aTempTime = deepClone(time).slice(0, 8);
    if(count > 3){
        for(let i = 0; i < count*2; i++){
            const addTime = moment(time.slice(-1)[0].time, 'HH:mm').add(0.5*(i+1), 'hour').format('HH:mm');
            aTempTime.push({ time: addTime, hide: false });
        }
    }
    return aTempTime;
}

//获取选中的站点
export function getSelectSite(siteList){
    const allSites = siteList.map(item => item.list).flat(1);
    const checkedSites = allSites.filter(ktem => ktem.checked);
    return checkedSites;
}

//获取天气文本和图片
export function getWeatherDetails(code){
    const details = {
        '1': { text: '阵雨转阴', icon: 'thunder.png' },
        '2': { text: '晴转阵雨', icon: 'shower.png' },
        '3': { text: '晴转阴', icon: 'cloudy.png' },
    }
    return details[code] || '';
}

//获取滚动条横向位移距离
export function getScrollX(str){
    const strArr = str.split(' ');
    let scrollX = 0;
    if(strArr.length > 1){
        const startPos = strArr[0].indexOf('(')+1, endPos = strArr[0].indexOf('px');
        scrollX = Number(strArr[0].substring(startPos, endPos));
    }
    return scrollX;
}